package com.third.clock.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.lzy.okgo.model.Response;
import com.third.clock.R;
import com.third.clock.bean.ResponseData;
import com.third.clock.common.Contants;
import com.third.clock.manager.ClockManager;
import com.third.clock.manager.OkHttpClientManager;
import com.third.clock.manager.TransformManager;
import com.third.clock.manager.WindowMessageManager;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Handler;

/**
 * Created by Administrator on 2018/7/26 0026.
 */

public class EditPassWordActivity extends Activity implements View.OnClickListener{

    private EditText mEditTextName;

    private EditText mEditTextPassword;

    private EditText mEditTextPasswordagin;

    private Button mButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editpassword);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_modify:
                editpassword();
                break;
        }
    }

    private void initView(){
        mEditTextName= (EditText) findViewById(R.id.et_userName);
        mEditTextPassword= (EditText) findViewById(R.id.et_password);
        mEditTextPasswordagin= (EditText) findViewById(R.id.et_passwordagin);
        mButton= (Button) findViewById(R.id.btn_modify);

        mButton.setOnClickListener(this);
    }

    private void editpassword(){
        ClockManager clockManager=ClockManager.getInstace(EditPassWordActivity.this);
        if(clockManager!=null){
            OkHttpClientManager okHttpClientManager=clockManager.getOkHttpClientManager();
            if(okHttpClientManager!=null){
                String username=mEditTextName.getText().toString().trim();
                if(TextUtils.isEmpty(username)){
                    showInfoWithStatus("用户名不为空");
                    return ;
                }
                if(!TransformManager.isMobileNO(username)){
                    showInfoWithStatus("手机号格式错误");
                    return;
                }
                String password=mEditTextPassword.getText().toString().trim();
                String passwordagin=mEditTextPasswordagin.getText().toString().trim();
                if(TextUtils.isEmpty(password)||TextUtils.isEmpty(passwordagin)){
                    showInfoWithStatus("密码不为空");
                    return;
                }
                if(!TransformManager.isPassword(password)||!TransformManager.isPassword(passwordagin)){
                    showInfoWithStatus("密码6位纯数字");
                    return;
                }
                if(!password.equals(passwordagin)){
                    showInfoWithStatus("两次输入密码不一致");
                    return;
                }

                Map<String,Object> map=new HashMap<>();
                map.put("mobile",username);
                map.put("password",password);
                String toSign=TransformManager.sortMapByKey(map);
                map.put("sign",toSign);

                okHttpClientManager.post(Contants.MODIFPWD_URL, map, new OkHttpClientManager.okHttpCallback() {
                    @Override
                    public void onFailure(Response<String> s) {
                        showInfoWithStatus("修改失败");
                    }

                    @Override
                    public void onResponse(Response<String> response) {
                        if(response==null&&response.body()!=null){
                            ResponseData<String> responseData=TransformManager.paserString(response.body());
                            if(responseData!=null&&responseData.getCode()==Contants.CODE_0){
                                delayedStartLogin();
                            }else{
                                showInfoWithStatus("修改失败");
                            }
                        }else{
                            showInfoWithStatus("修改失败");
                        }
                    }
                });

            }
        }
    }

    private void delayedStartLogin(){
        showSuccessWithStatus("修改成功");
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(EditPassWordActivity.this,LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                EditPassWordActivity.this.finish();
            }
        },2000);
    }

    private void showInfoWithStatus(String string){
        if(!TextUtils.isEmpty(string)) {
            WindowMessageManager.getInstance().showInfoWithStatus(EditPassWordActivity.this,string);
        }
    }

    private void showSuccessWithStatus(String string){
        if(!TextUtils.isEmpty(string)) {
            WindowMessageManager.getInstance().showSuccessWithStatus(EditPassWordActivity.this,string);
        }
    }
}
